﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using JinianNet.JNCms.Filters;
using JinianNet.JNCms.Models;


namespace JinianNet.JNCms.Controllers
{
    [ManagerFilter()]
    public class LinksController : ControllerBase
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List(int item=0)
        {
            List<Links> list = Data.DataAccess.CreateLinks().GetList(item);
            var array = Data.DataAccess.CreateClassify().GetList(0, 2);
            array.Insert(0, new Classify());
            array[0] = new Classify();
            array[0].ClassType = 2;
            array[0].CreateTime = DateTime.Now;
            array[0].ID = 0;
            array[0].ParentID = 0;
            array[0].Title = "默认分类";
            array[0].UserID = 0;
            SelectList items = new SelectList(array, "ID", "Title", item);
            ViewData["ClassID"] = items;
            return View(list);
        }

        //[Authorize(Users="")]
        public ActionResult Edit(int item)
        {
            Links model = Data.DataAccess.CreateLinks().GetItem(item);
            if (model == null)
                return PageNotFound();
            var array = Data.DataAccess.CreateClassify().GetList(0, 2);
            array.Insert(0, new Classify());
            array[0] = new Classify();
            array[0].ClassType = 2;
            array[0].CreateTime = DateTime.Now;
            array[0].ID = 0;
            array[0].ParentID = 0;
            array[0].Title = "默认分类";
            array[0].UserID = 0;
            SelectList items = new SelectList(array, "ID", "Title", model.ClassID);
            ViewData["ClassID"] = items;
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        //[Authorize(Users = "")]
        public ActionResult Edit(Links model)
        {
            if (Data.DataAccess.CreateLinks().Update(model) > 0)
            {
                return Content(Config.GetTips("恭喜，编辑成功", Url.Action("List", "Links", new { item = model.ClassID }), true));
            }
            else
            {
                return Content(Config.GetTips("编辑失败，请重试", Url.Action("Edit", "Links", new { model = model }), false));
            }
        }

        [HttpPost]
        [ValidateInput(false)]
        //[Authorize(Users = "")]
        public ActionResult Insert(Links model)
        {
            model.CreateTime = DateTime.Now;
            Data.DataAccess.CreateLinks().Insert(model);
            return Content(Config.GetTips("恭喜，操作成功", Url.Action("List", "Links", new { item = model.ClassID }), true));
        }

        //[Authorize(Users = "")]
        public ActionResult Insert(int item=0)
        {
            Links model = new Links();
            model.ClassID = item;
            model.CreateTime = DateTime.Now;
            var array = Data.DataAccess.CreateClassify().GetList(0, 2);
            array.Insert(0, new Classify());
            array[0] = new Classify();
            array[0].ClassType = 2;
            array[0].CreateTime = DateTime.Now;
            array[0].ID = 0;
            array[0].ParentID = 0;
            array[0].Title = "默认分类";
            array[0].UserID = 0; 
            SelectList items = new SelectList(array, "ID", "Title", item);
            ViewData["ClassID"] = items;
            return View("Edit", model);
        }

        public ActionResult Delete(string item)
        {
            Data.ILinks dal = Data.DataAccess.CreateLinks();
            if (item.IndexOf(',') < 0)
                dal.Delete(int.Parse(item));
            else
                foreach (string value in item.Split(','))
                    if (!string.IsNullOrEmpty(value))
                        dal.Delete(int.Parse(value));
            return Content(Config.GetTips("操作成功", null, true));
        }
    }
}
